//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    visitTotal_num:19119,
    starCount_num: 0,
    forksCount_num: 0,
    starCount: 0,
    forksCount: 0,
    visitTotal: 99999,
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    CustomBar2: 0,
    Displaytime: 50,
    Notice: "代取1元1一件（小件），体验价5毛。每晚8.30点配送，下单加QQ群关注配送信息，QQ群:954508637",
    motto: 'Hi 开发者！',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),

    animation: false,
    cardCur: 0,
    "swiperList": [{
        "id": 1,
        "type": "image",
        "url": "https://ae01.alicdn.com/kf/H830709731af54a9d9a01f12fba80b70ag.jpg",
        "zIndex": 2,
        "mLeft": -2
      },
      {
        "id": "3",
        "type": "image",
        "url": "https://ae01.alicdn.com/kf/H61d69f2a71fd4139b39d6222fa7808b3t.jpg",
        "zIndex": 3,
        "mLeft": -1
      },
      {
        "id": "2",
        "type": "image",
        "url": "https://ae01.alicdn.com/kf/H500f95bc7c7e40448f72ad9fe6b14db6D.jpg",
        "zIndex": "1",
        "mLeft": 0
      }
    ],
    // swiperList: [{
    //   id: 0,
    //   type: 'image',
    //   url: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1573448545979&di=351e3b1f1f79ddc33eea5b3a8c9abfd0&imgtype=0&src=http%3A%2F%2Fimage.tianjimedia.com%2FuploadImages%2Fupload%2F20141112%2Fg15qcyogpkcjpg.jpg'
    // }, {
    //   id: 1,
    //   type: 'image',
    //   url: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1573448592018&di=c888c8fecf8ee6fa4e1eecb19007b724&imgtype=0&src=http%3A%2F%2Fc.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2Fe1fe9925bc315c609e37b84f86b1cb13485477f4.jpg',
    // }, {
    //   id: 2,
    //   type: 'image',
    //   url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big39000.jpg'
    // }, {
    //   id: 3,
    //   type: 'image',
    //   url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big10001.jpg'
    // }, {
    //   id: 4,
    //   type: 'image',
    //   url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big25011.jpg'
    // }, {
    //   id: 5,
    //   type: 'image',
    //   url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big25011.jpg'
    // }, {
    //   id: 6,
    //   type: 'image',
    //   url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big99008.jpg'
    // }],
    "expressList": [{
        "express_company": "中通-西区",
        "express_company_id": 6,
        "shopaddres": "商家详细地址",
        "shopid": 1006,
        "shopname": "中通对应存放店铺名称",
        "shopphone": 13281212112,
        "type": "image",
        "url": "../../images/zt.png"
      },
      {
        "express_company": "圆通-西区",
        "express_company_id": 4,
        "shopaddres": "商家详细地址",
        "shopid": 1004,
        "shopname": "圆通对应存放店铺名称",
        "shopphone": 13281212112,
        "type": "image",
        "url": "../../images/yt.png"
      },
      {
        "express_company": "申通-西区",
        "express_company_id": 5,
        "shopaddres": "商家详细地址",
        "shopid": 1005,
        "shopname": "申通对应存放店铺名称",
        "shopphone": 13281212112,
        "type": "image",
        "url": "../../images/st.png"
      },
      {
        "express_company": "韵达-西区",
        "express_company_id": 2,
        "shopaddres": "商家详细地址",
        "shopid": 1002,
        "shopname": "韵达对应存放店铺名称",
        "shopphone": 13281212112,
        "type": "image",
        "url": "../../images/yd.png"
      },
      {
        "express_company": "天天-西区",
        "express_company_id": 7,
        "shopaddres": "商家详细地址",
        "shopid": 1007,
        "shopname": "天邮",
        "shopphone": 13281212112,
        "type": "image",
        "url": "../../images/tt.png"
      },
      {
        "express_company": "邮政-西区",
        "express_company_id": 8,
        "shopaddres": "商家详细地址",
        "shopid": 1008,
        "shopname": "天邮",
        "shopphone": 13281212112,
        "type": "image",
        "url": "../../images/yz.png"
      },
      {
        "express_company": "百世-西区",
        "express_company_id": 9,
        "shopaddres": "商家详细地址",
        "shopid": 1009,
        "shopname": "百顺苏",
        "shopphone": 13281212112,
        "type": "image",
        "url": "../../images/bs.png"
      },
      {
        "express_company": "顺丰-西区",
        "express_company_id": 0,
        "shopaddres": "顺丰详细地址",
        "shopid": 1000,
        "shopname": "百顺苏",
        "shopphone": 13281212112,
        "type": "image",
        "url": "../../images/sf.png"
      },
      {
        "express_company": "苏宁-西区",
        "express_company_id": 3,
        "shopaddres": "商家详细地址",
        "shopid": 1003,
        "shopname": "百顺苏",
        "shopphone": 13281212112,
        "type": "image",
        "url": "../../images/sn.png"
      },
      {
        "express_company": "京东-西区",
        "express_company_id": 1,
        "shopaddres": "商家详细地址",
        "shopid": 1001,
        "shopname": "京东对应存放店铺名称",
        "shopphone": 13281212112,
        "type": "image",
        "url": "../../images/jd.png"
      }
    ],
    "post_expressList": [{
      "express_company": "寄快递-免费上门",
      "express_company_id": 18,
      "shopaddres": "商家详细地址",
      "shopid": 1006,
      "shopname": "对应存放店铺名称",
      "shopphone": 13281212112,
      "type": "image",
      "url": "../../images/post.png"
    }
    // ,
    // {
    //   "express_company": "圆通-西区",
    //   "express_company_id": 4,
    //   "shopaddres": "商家详细地址",
    //   "shopid": 1004,
    //   "shopname": "圆通对应存放店铺名称",
    //   "shopphone": 13281212112,
    //   "type": "image",
    //   "url": "../../images/yt.png"
    // } 
    ],
    // expressList: [{
    //   express_company_id: 0,
    //   shopname: "顺丰对应存放店铺名称",
    //   shopid: 1000,
    //   shopphone: 13281212112,
    //   shopaddres: "商家详细地址",
    //   express_company: '顺丰-西区',
    //   url: '../../images/sf.png',
    //   type: "image"

    // }, {
    //   express_company_id: 1,
    //   shopname: "京东对应存放店铺名称",
    //   shopid: 1001,
    //   shopphone: 13281212112,
    //   shopaddres: "商家详细地址",
    //     express_company: '京东-西区',
    //   url: '../../images/jd.png',
    //   type: "image"
    // }, {
    //   express_company_id: 2,
    //   shopname: "京东对应存放店铺名称",
    //   shopid: 1002,
    //   shopphone: 13281212112,
    //   shopaddres: "商家详细地址",
    //     express_company: '韵达-西区',
    //   url: '../../images/yd.png',
    //   type: "image"
    // }, {
    //   express_company_id: 3,
    //   shopname: "苏宁对应存放店铺名称",
    //   shopid: 1003,
    //   shopphone: 13281212112,
    //   shopaddres: "商家详细地址",
    //     express_company: '苏宁-西区',
    //   url: '../../images/sn.png',
    //   type: "image"
    // }, {
    //   express_company_id: 4,
    //   shopname: "圆通对应存放店铺名称",
    //   shopid: 1004,
    //   shopphone: 13281212112,
    //   shopaddres: "商家详细地址",
    //   express_company: '圆通-西区',
    //   url: '../../images/yt.png',
    //   type: "image"
    // }, {
    //   express_company_id: 5,
    //   shopname: "申通对应存放店铺名称",
    //   shopid: 1005,
    //   shopphone: 13281212112,
    //   shopaddres: "商家详细地址",
    //   express_company: '申通-西区',
    //   url: '../../images/st.png',
    //   type: "image"
    // }, {
    //   express_company_id: 6,
    //   shopname: "顺丰对应存放店铺名称",
    //   shopid: 1006,
    //   shopphone: 13281212112,
    //   shopaddres: "商家详细地址",
    //   express_company: '中通-西区',
    //   url: '../../images/zt.png',
    //   type: "image"
    // }, {
    //   express_company_id: 7,
    //   shopname: "顺丰对应存放店铺名称",
    //   shopid: 1007,
    //   shopphone: 13281212112,
    //   shopaddres: "商家详细地址",
    //   express_company: '天天-西区',
    //   url: '../../images/tt.png',
    //   type: "image"
    // }, {
    //   express_company_id: 8,
    //   shopname: "顺丰对应存放店铺名称",
    //   shopid: 1008,
    //   shopphone: 13281212112,
    //   shopaddres: "商家详细地址",
    //   express_company: '邮政-西区',
    //   url: '../../images/yz.png',
    //   type: "image"
    // }, {
    //   express_company_id: 9,
    //   shopname: "顺丰对应存放店铺名称",
    //   shopid: 1009,
    //   shopphone: 13281212112,
    //   shopaddres: "商家详细地址",
    //   // icon: 'brandfill',
    //   // color: 'mauve',
    //   // badge: 0,
    //   express_company: '中通-西区',
    //   url: '../../images/zt.png',
    //   type: "image"
    // }],
    gridCol: 5,
    skin: false,
    attr_control_1:false
   

  },
  coutNum(e) {
    if (e > 1000 && e < 10000) {
      e = (e / 1000).toFixed(1) + 'k'
    }
    if (e > 10000) {
      e = (e / 10000).toFixed(1) + 'W'
    }
    return e
  },
  test_2(res) {
    const db = wx.cloud.database()
    const _ = db.command.aggregate
    let res2 = db.collection('oder_Lists').aggregate()
      .group({
        _id: "$_openid",//分组类别前面必须加“$”
        num: _.sum(1)   
      })
      .sort({
        num: -1 
       //  1 代表升序排列（从小到大）； - 1 代表降序排列（从大到小）；
      })
      .end({
        success(res){
          console.log(res)

        }
      })
  },
  copy_link(res) {
    wx.setClipboardData({
      data: res.currentTarget.dataset.link,
      success: res => {
        wx.showToast({
          title: 'QQ群号已复制:954508637，快去寻找组织吧！',
          icon: "none",
          duration: 1500,
        })
      }
    })
  },
  detail(e) {
    console.log(e)
    this.setData({
      modalName: e.currentTarget.dataset.target
    })

    // wx.showToast({
    //   title: '详情功能尚未完善~',
    //   icon: "none"
    // })
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  gridchange: function(e) {
    this.setData({
      gridCol: e.detail.value
    });
    wx.setStorageSync("gridCol", e.detail.value)
  },
  gridswitch: function(e) {
    this.setData({
      gridBorder: e.detail.value
    });
    wx.setStorageSync("gridBorder", e.detail.value)

  },

  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  show_yx: function(res) {
    // wx.showToast({
    //   title: '延旭未来，改变既是现在！',
    //   icon: "none",
    //   duration: 1500
    // })
    wx.setClipboardData({
      data: res.currentTarget.dataset.link,
      success: res => {
        wx.showToast({
          title: '延旭未来，改变既是现在！微信号已复制:18580015335',
          icon: "none",
          duration: 1000,
        })
      }
    })
  },

  onLoad: function() {
    var that = this
    // 在页面中定义激励视频广告
// let videoAd = null

// // 在页面onLoad回调事件中创建激励视频广告实例
// if (wx.createRewardedVideoAd) {
//   videoAd = wx.createRewardedVideoAd({
//     adUnitId: 'adunit-59f154d5ea3951be'
//   })
//   videoAd.onLoad(() => {})
//   videoAd.onError((err) => {})
//   videoAd.onClose((res) => {})
// }

// // 用户触发广告后，显示激励视频广告
// if (videoAd) {
//   videoAd.show().catch(() => {
//     // 失败重试
//     videoAd.load()
//       .then(() => videoAd.show())
//       .catch(err => {
//         console.log('激励视频 广告显示失败')
//       })
//   })
// }
    console.log("success")
    // let that = this;
    wx.showLoading({
      title: '数据加载中',
      mask: true,
    })
    let i = 0;
    numDH();
    function numDH() {
      if (i < 20) {
        setTimeout(function () {
          that.setData({
            starCount: i,
            forksCount: i,
            visitTotal: i
          })
          i++
          numDH();
        }, 20)
      } else {
        that.setData({
          starCount: that.coutNum(that.data.starCount_num),
          forksCount: that.coutNum(that.data.forksCount_num),
          visitTotal: that.coutNum(that.data.visitTotal_num)
        })
      }
    }
    wx.hideLoading()
    // 
    // 获取配置信息
    const db = wx.cloud.database()
    const _ = db.command
    let curDate = new Date();
    console.log(curDate)
    db.collection("configs").where({
      _id: "b75ec551-eddd-4830-a830-5af35af0c7cb"
      // _openid: "ohi2m5ITTPWn8NRlT9g5bi3xzJlE"
    }).get({
      success(res) {
        console.log(res.data.length)
        if (res.data.length > 0) {
          wx.setStorageSync("configs", res.data[0])
          var configs = wx.getStorageSync("configs")
          console.log(configs.Notice)

          that.setData({

            Notice: configs.Notice,
            motto: configs.motto,
            userInfo: configs.userInfo?configs.userInfo:"null",
            hasUserInfo: configs.hasUserInfo,

            animation: configs.animation,
            cardCur: configs.cardCur,
            swiperList: configs.swiperList,
            expressList: configs.expressList,
            //gridCol: configs.gridCol, //每一列展示个数
            gridCol: wx.getStorageSync("gridCol") ? wx.getStorageSync("gridCol") : 5,
            gridBorder: wx.getStorageSync("gridBorder") ? wx.getStorageSync("gridBorder") : false,

            skin: configs.skin,
            visitTotal_num: configs.visitTotal_num,
            starCount_num: configs.starCount_num,
            forksCount_num: configs.forksCount_num,
            // 控制邮寄模块
            attr_control_1:configs.attr_control_1

          })
 
        }
        console.log(res.data[0])

        console.log(res)
      },
      fail(res) {
        console.log(res)

      },
      complete(res) {
        console.log(res)
        // 关闭加载中！

      }


    })

    this.towerSwiper('swiperList');
    // 初始化towerSwiper 传已有的数组名即可
    console.log(this.data.Notice.length)

    setTimeout(() => {
      this.setData({
        animation: true,
        Displaytime: this.data.Notice.length / 3,
        Displaylength: -(this.data.Notice.length / 21)
      })
    }, 600)
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  DotStyle(e) {
    this.setData({
      DotStyle: e.detail.value
    })
  },
  // cardSwiper
  cardSwiper(e) {
    this.setData({
      cardCur: e.detail.current
    })
  },
  // towerSwiper
  // 初始化towerSwiper
  towerSwiper(name) {
    let list = this.data[name];
    for (let i = 0; i < list.length; i++) {
      list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
      list[i].mLeft = i - parseInt(list.length / 2)
    }
    this.setData({
      swiperList: list
    })
  },
  // towerSwiper触摸开始
  towerStart(e) {
    this.setData({
      towerStart: e.touches[0].pageX
    })
  },
  // towerSwiper计算方向
  towerMove(e) {
    this.setData({
      direction: e.touches[0].pageX - this.data.towerStart > 0 ? 'right' : 'left'
    })
  },
  // towerSwiper计算滚动
  towerEnd(e) {
    let direction = this.data.direction;
    let list = this.data.swiperList;
    if (direction == 'right') {
      let mLeft = list[0].mLeft;
      let zIndex = list[0].zIndex;
      for (let i = 1; i < list.length; i++) {
        list[i - 1].mLeft = list[i].mLeft
        list[i - 1].zIndex = list[i].zIndex
      }
      list[list.length - 1].mLeft = mLeft;
      list[list.length - 1].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    } else {
      let mLeft = list[list.length - 1].mLeft;
      let zIndex = list[list.length - 1].zIndex;
      for (let i = list.length - 1; i > 0; i--) {
        list[i].mLeft = list[i - 1].mLeft
        list[i].zIndex = list[i - 1].zIndex
      }
      list[0].mLeft = mLeft;
      list[0].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    }
  },
  // 点击按钮进入订单页面
  to_oder(e) {
    console.log(e)
    wx.showLoading({
      title: '客官稍等！',
    })
    //return
    wx.navigateTo({
      url: '/pages/placeorder/placeorder?express_company_id=' + e.currentTarget.dataset.expressinfo.express_company_id + '&express_company=' + e.currentTarget.dataset.expressinfo.express_company + '&shopname=' + e.currentTarget.dataset.expressinfo.shopname,
    })




  },
  to_post(e) {
    console.log(e)
    wx.showLoading({
      title: '客官稍等！',
    })
    //return
    wx.navigateTo({
      url: '/pages/post/post?express_company_id=' + e.currentTarget.dataset.expressinfo.express_company_id + '&express_company=' + e.currentTarget.dataset.expressinfo.express_company + '&shopname=' + e.currentTarget.dataset.expressinfo.shopname,
    })




  },
  // 页面关闭
  onHide(res) {
    wx.hideLoading()
  },
  onShareAppMessage: function() {

  },
})